import httpUtil from "../utils/httpUtil";
import logApi from "./logApi";

const getMiniLoginCode = (callBack) => {
	uni.getProvider({
		service: 'payment',
		success: res => {
			console.log('getProvider success', res);
			console.log(res.provider[0]);
			uni.login({
				provider: res.provider[0],
				success: loginRes => {
					// console.log("loginRes.code", loginRes.code);
					// getUserIdByServe(loginRes.code);
					if (callBack) {
						callBack(loginRes.code)
					}
				},
				fail: function(loginFail) {
					console.log("loginFail", loginFail);
				}
			});
		},
		fail: res => {
			console.log('getProvider fail', res);
		}
	})
}

const initUserId = () => {
	getMiniLoginCode(loginCode => {
		let params = {
			code: loginCode,
			pro_id: 51
		}
		httpUtil.post('/api/rf/getAliUserid', params, res => {
			logApi.addLog('获取openid，成功。请求参数：' + JSON.stringify(params) + '。返回参数:' + JSON.stringify(
				res))
			console.log('initUserId', res)
			setUserId(res.data.user_id)
		}, err => {
			logApi.addLog('获取openid，失败。请求参数：' + JSON.stringify(params) + '。返回参数:' + JSON.stringify(
				err))
		})
	})

}

const setUserId = (value) => {
	uni.setStorageSync('userId', value)
}

const getUserId = () => {
	return uni.getStorageSync('userId')
}

const setParam = (value) => {
	uni.setStorageSync('param', value)
}

const getParam = () => {
	return uni.getStorageSync('param')
}

export default {
	initUserId,
	setUserId,
	getUserId,
	setParam,
	getParam,
}